package com.paomi.client.activity.payMoney;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
import com.paomi.client.R;
import com.paomi.client.activity.detail.OrderDetailActivity;
import com.paomi.client.activity.payMoney.model.PayMoneyBean;
import com.paomi.client.activity.payResult.ResultPayActivity;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.PayResult;
import com.paomi.client.utils.PayUtil;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.widget.RxToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by pc on 2018/11/14
 * Describe:支付订单页面
 * com.paomi.client.activity.payMoney
 */
public class PayMoneyPresenter extends MvpBasePresenter<PayMoneyView> {
    /**
     * 支付订单接口
     *
     * @param type
     * @param getId orderId	true	订单号
     *              type	true	支付类型 1 微信 2 支付宝
     */
    public void payIsCan(int type, String getId, Activity activity) {
        mActivity = activity;
        Map<String, String> maps = new HashMap<>();
        maps.put("orderId", getId);
        maps.put("type", "" + type);
        Api.getInstanceService(Constants.Pay_URL).sureOrderPay(maps).enqueue(new BaseCallback<PayMoneyBean>() {
            @Override
            public void onResponse(Call<PayMoneyBean> call, PayMoneyBean result) {
                if(result.getReturnData()!=null) {
                    if (type == 1) {//微信的
//                        RxToast.info("微信的: " + result.getReturnData().toString());
                        Map<String,String> map=(Map<String, String>) result.getReturnData();
                        PayUtil.payForWx(activity, null, map);
                    } else if (type == 2) {//支付宝
                        payApi_pay(result.getReturnData().toString(), activity);
                    }
                }
            }
        });
    }

    private Activity mActivity;

    /**
     * 支付宝支付
     *
     * @param data
     * @param activity
     */
    public void payApi_pay(String data, Activity activity) {
//        final String payInfo = data.get("action");

        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                // 构造PayTask 对象
                PayTask alipay = new PayTask(activity);
                // 调用支付接口，获取支付结果
                Map<String, String> result = alipay.payV2(data, true);

                Message msg = new Message();
                msg.what = 1;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PayResult payResult = new PayResult((Map<String, String>) msg.obj);
            /**
             对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
             */
            String resultInfo = payResult.getResult();// 同步返回需要验证的信息
            String resultStatus = payResult.getResultStatus();
            // 判断resultStatus 为9000则代表支付成功
            if (TextUtils.equals(resultStatus, "9000")) {//支付成功
                // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                ToastUtil.getToastEmail().ToastShow(mActivity, null, 1, R.layout.toast_view, mActivity.getResources().getString(R.string.pay_sure));
                mActivity.startActivity(new Intent(mActivity, ResultPayActivity.class));
                mActivity.finish();

            } else {//支付失败
                Log.e("支付失败",""+payResult);
                // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                RxToast.info(mActivity.getResources().getString(R.string.pay_cancel_get));
            }

        }

        ;
    };


}
